# To change this template, choose Tools | Templates
# and open the template in the editor.
require 'win32ole'
require 'util'

class Word
  @@word = nil
  def initialize(file_path)
    @file_path = file_path
    if (!@@word)
      @@word=WIN32OLE.new("word.application")
      @@word['Visible']=TRUE
    end
  end
  def open(mode)
    @workbook = @@word.documents.open @file_path
    @selection = @@word.Selection
  end
  def write(text)
    @workbook.Range.select
    @@word.selection.insertafter(text)
#    puts "write edit in document"
  end
  def find(find_text)
    @workbook.Range.select
    @selection.find.text = find_text
    result = @selection.find.execute
    return result
  end
  def match(find_text, conditions, results)
    puts "find? #{find(find_text)}"
    command = @@word
    Util.command_send(command, conditions, results)
  end
  def check_answer(answer_lines)
    answer_count = 0
    answer_lines.each { |answer|
      if (match(answer[0], answer[1], answer[2]))
        puts "#{answer[0]}, #{answer[1]}, #{answer[2]} match"
        answer_count += 1
      else
        puts "#{answer[0]}, #{answer[1]}, #{answer[2]} not match"
      end
    }
    return answer_count
  end
  def close
    #    puts "close word #{@file_path}"
    @workbook.close
  end
end
